package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.SysUserDao;
import com.example.dao.SysUserRoleDao;
import com.example.entity.SysRole;
import com.example.entity.SysUser;
import com.example.entity.vo.UserVO;
import com.example.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2022-04-11 22:35:36
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 根据用户ID获取当前用户的角色和菜单
     * @param uid
     * @return
     */
    @Override
    public String getUserAuthorityInfo(Integer uid) {
        List<SysRole> roles = sysUserRoleDao.selectRoles(uid);
        List<Integer> navMenuIds = sysUserDao.getNavMenuIds(uid);
        return roles.stream().map(role -> "ROLE_" + role.getCode()).collect(Collectors.joining(","));
    }

    /**
     * 根据用户名获取用户info
     * @param username
     * @return
     */
    @Override
    public SysUser getByUsername(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        SysUser user = baseMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public UserVO getSysUser(String username) {
        UserVO userVO = new UserVO();
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        SysUser user = baseMapper.selectOne(wrapper);
        userVO.setId(user.getId());
        userVO.setUsername(username);
        userVO.setAvatar(user.getAvatar());
        List<SysRole> sysRoles = sysUserRoleDao.selectRoles(user.getId());
        userVO.setRoles(sysRoles);
        return userVO;
    }
}

